/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web.diva.server.model;

import java.awt.Color;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author Yehia Farag
 */
public class JFreeImgGenerator {

    public synchronized String saveToFile(JFreeChart chart, double width, double height, String path, ChartRenderingInfo chartRenderingInfo, String imgName) {
        byte imageData[] = null;
        try {
            if (chartRenderingInfo != null) {
                imageData = ChartUtilities.encodeAsPNG(chart.createBufferedImage((int) width, (int) height, chartRenderingInfo));
                System.gc();
            } else {
                imageData = ChartUtilities.encodeAsPNG(chart.createBufferedImage((int) width, (int) height));
                System.gc();
            }
            String base64 = Base64.encodeBase64String(imageData);
            base64 = "data:image/png;base64," + base64;
            return base64;
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        return "";

    }

    public Color hex2Rgb(String colorStr) {
        return new Color(
                Integer.valueOf(colorStr.substring(1, 3), 16),
                Integer.valueOf(colorStr.substring(3, 5), 16),
                Integer.valueOf(colorStr.substring(5, 7), 16));
    }

}
